<template>
  <div class="model">
    <div class="mask" @click="changeIsShowLoginModel(false)"></div>
    <div class="loginbox">
      <div class="close" @click="changeIsShowLoginModel(false)"></div>
      <section class="nav">
        <a href="#" :class="{ active: isShowPhone }" @click="isShowPhone = true"
          >手机号登录</a
        >|
        <a href="#" :class="{ active: !isShowPhone }" @click="showQrCode"
          >微信扫码登录</a
        >
      </section>
      <div class="content">
        <div class="phonelogin" v-show="isShowPhone">
          <div class="row">
            <input
              type="text"
              class="ipt"
              placeholder="请输入手机号"
              v-model="phoneNumber"
            />
          </div>
          <div class="row">
            <slide-verify
              :l="42"
              :r="20"
              :w="362"
              :h="140"
              @success="onSuccess"
              @fail="onFail"
              @refresh="onRefresh"
              :style="{ width: '100%' }"
              class="slide-box"
              ref="slideBlock"
              :slider-text="msg"
              :imgs="arr"
            ></slide-verify>
            <!-- <div>{{ msg }}</div> -->
          </div>
          <div class="row">
            <input
              type="text"
              class="ipt"
              placeholder="请输入验证码"
              v-model="code"
            />
            <button class="btncode" @click="getCode" v-show="!isShowCount">
              发送验证码
            </button>
            <button class="btncode" v-show="isShowCount">
              {{ count + "s" }}
            </button>
          </div>
          <div class="row">
            <button class="btnlogin" @click="subumit">登录</button>
          </div>
        </div>
        <div v-show="!isShowPhone" id="weixin" @click="showQrCode">
          微信扫码登录
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { SendSMSAPI, PhoneReginAPI, BindingAPI } from "../request/api";
import { validatePhoneNumber } from "../Utils/index";
export default {
  data() {
    return {
      isShowPhone: true,
      arr: [
        require("@/assets/img/integral-01.9386d4bf.png"),
        require("@/assets/img/integral-02.150d92a1.png"),
      ],
      msg: "向右滑动",
      phoneNumber: "",
      count: 59,
      isShowCount: false,
      code: "",
    };
  },

  computed: {
    ...mapState({
      isShowLoginModal: (state) => state.showModel.isShowLoginModal,
      // isShowToast: (state) => state.showToast.isShowToast,
    }),
  },

  methods: {
    ...mapMutations({
      changeIsShowLoginModel: "showModel/changeIsShowLoginModel",
      changeIshowLogin: "LoginStatus/changeIshowLogin",
      changeisShowToast: "showToast/changeisShowToast",
    }),
    ...mapActions({
      asyncchangeisShowToast: "showToast/asyncchangeisShowToast",
      asyupdateUser: "UserInfo/asyupdateUser",
    }),
    showQrCode() {
      this.isShowPhone = false;
      //获取二维码 并展示
      // 申请微信登录二维码
      let _this = this;
      new WxLogin({
        id: "weixin",
        appid: "wx67cfaf9e3ad31a0d", // 这个appid要填死
        scope: "snsapi_login",
        // 扫码成功后重定向的接口
        redirect_uri: "https://sc.wolfcode.cn/cms/wechatUsers/shop/PC",
        // state填写编码后的url
        state: encodeURIComponent(
          window.btoa("http://127.0.0.1:8080" + _this.$route.path)
        ),
        // 调用样式文件
        href: "data:text/css;base64,LmltcG93ZXJCb3ggLnRpdGxlLA0KLmltcG93ZXJCb3ggLmluZm8gew0KICAgIGRpc3BsYXk6IG5vbmU7DQp9",
      });
    },
    // 拼图成功
    onSuccess(times) {
      let ms = (times / 1000).toFixed(1);
      this.msg = "login success, 耗时 " + ms + "s";
    },
    // 拼图失败
    onFail() {
      this.onRefresh(); // 重新刷新拼图
    },
    // 拼图刷新
    onRefresh() {
      this.msg = "再试一次";
    },
    daojishi() {
      this.isShowCount = true;
      let timers = setInterval(() => {
        this.count--;
        if (this.count <= 0) {
          clearInterval(timers);
          this.count = 59;
          this.isShowCount = false;
        }
      }, 1000);
    },
    toVerify() {
      if (!validatePhoneNumber(this.phoneNumber)) {
        // alert("请输入正确手机号");
        // this.changeisShowToast({
        //   isShowToast: true,
        //   msg: "请输入正确手机号",
        //   type: "success",
        // });
        // setTimeout(() => {
        //   this.changeisShowToast({
        //     isShowToast: false,
        //   });
        // }, 1000);
        this.asyncchangeisShowToast({
          isShowToast: true,
          msg: "请输入正确手机号",
          type: "success",
        });
        return;
      }
      if (this.msg == "再试一次" || this.msg == "向右滑动") {
        // alert("请滑动滑动验证码");
        this.asyncchangeisShowToast({
          isShowToast: true,
          msg: "请滑动滑动验证码",
          type: "danger",
        });
        return;
      }
      return true;
    },
    async getCode() {
      if (!this.toVerify()) return;

      let res = await SendSMSAPI({
        phone: this.phoneNumber,
      });
      //   console.log("res5", res);
      // if (res.data.code == 0) {
      if (!res) return;
      // alert("发送成功");
      this.asyncchangeisShowToast({
        isShowToast: true,
        msg: "发送成功",
        type: "success",
      });
      this.daojishi();
    },
    async subumit() {
      if (!this.toVerify()) return;
      if (!this.code.trim()) {
        // alert("请输入手机验证码");
        this.asyncchangeisShowToast({
          isShowToast: true,
          msg: "请输入手机验证码",
          type: "danger",
        });
        return;
      }
      // 发起登录请求
      let uuid = localStorage.getItem("uuid");
      let res = null;
      if (uuid) {
        res = await BindingAPI({
          verifyCode: this.code,
          phone: this.phoneNumber,
          uuid,
        });
      } else {
        res = await PhoneReginAPI({
          verifyCode: this.code,
          phone: this.phoneNumber,
        });
      }

      // console.log("res5", res);
      localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
      // 关闭登陆款
      this.changeIsShowLoginModel(false);
      // touken中登录状态改变
      this.changeIshowLogin(true);

      uuid && this.$router.push(this.$route.path);

      localStorage.removeItem("uuid");

      this.asyupdateUser();
    },
  },
  // },
};
</script>

<style lang="less" scoped>
.model {
  position: fixed;
  width: 100%;
  height: 100%;
  left: 0;
  top: 0;
  .mask {
    position: relative;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    background-color: rgba(0, 0, 0, 0.5);
  }
  .loginbox {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    width: 555px;
    height: 423px;
    background: url("../assets/img/login-box-bg.png");
    .close {
      cursor: pointer;
      position: absolute;
      width: 22px;
      height: 22px;
      right: 60px;
      top: 21px;
      background: url("@/assets/img/close.png");
    }
    .nav {
      text-align: center;
      padding-top: 30px;
      a {
        text-decoration: none;
        color: gray;
        &.active {
          color: black;
        }
      }
    }
    .content {
      width: 355px;
      height: 300px;
      margin: 30px auto;
      .phonelogin {
        .row {
          display: flex;
          margin-bottom: 20px;
          .ipt {
            height: 50px;
            flex: 1;
            outline: none;
            text-indent: 1em;
            font-size: 16px;
          }
          .btncode {
            margin-left: 10px;
            background-color: #0a328e;
            color: #fff;
            border: none;
            cursor: pointer;
            width: 85px;
          }
          .btnlogin {
            background-color: #0a328e;
            color: #fff;
            width: 100%;
            height: 50px;
            border: none;
            cursor: pointer;
          }
        }
      }
    }
  }
}

/deep/.slide-box {
  width: 100%;
  position: relative;
  box-sizing: border-box;
  canvas {
    position: absolute;
    left: 0;
    top: -120px;
    display: none;
    width: 100%;
    box-sizing: border-box;
  }
  .slide-verify-block {
    width: 85px;
    height: 136px;
  }
  .slide-verify-refresh-icon {
    top: -120px;
    display: none;
  }
  &:hover {
    canvas {
      display: block;
    }
    .slide-verify-refresh-icon {
      display: block;
    }
  }
}

#weixin {
  text-align: center;
}
</style>